<template>
  <div class="community">
    <!-- <navBar></navBar> -->
    <!-- tab栏 -->
    <div class="tab">
      <van-tabs
        v-model="nameActive"
        swipeable
        line-height="0px"
        @click="attention"
      >
        <van-tab :name="0">
          <template #title>
            <div class="title" :class="{ active: 0 === nameActive }">热门</div>
          </template>
        </van-tab>
        <van-tab :name="1">
          <template #title>
            <div class="title" :class="{ active: currentIndex === 1 }">
              关注
            </div>
          </template>
        </van-tab>
      </van-tabs>
    </div>
    <!-- 球类 -->
    <div v-if="this.currentIndex === 0">
      <van-pull-refresh
        v-model="starLoading"
        @refresh="onFrech"
        success-text="刷新成功"
      >
        <div class="hot-group">
          <div class="list">
            <div class="item" v-for="(item, index) in ballList" :key="index">
              <img class="img" :src="item.communityIcon" alt="" />
              <div class="itemName">{{ item.name }}</div>
            </div>
          </div>
        </div>
        <!-- 精贴 -->
        <div>
          <div class="hot-post">
            <div
              class="row-post"
              v-for="(item1, index) in goodnote"
              :key="index"
            >
              <div class="good">
                <div class="type"><i class="iconfont">&#xe606;</i>精贴</div>
              </div>
              <div class="title">{{ item1.title }}</div>
            </div>
          </div>
          <!-- 热门榜 -->
          <van-list
            v-model="upLoading"
            :finished="finished"
            finished-text="没有更多了"
            @load="upLoad"
          >
            <div class="hot-content">
              <div
                class="item-content"
                v-for="(item2, index) in community"
                :key="index"
              >
                <div class="name-active">
                  <img class="img" :src="item2.userImage" alt="" />
                  <div class="N1">
                    <div class="name">{{ item2.userName }}</div>
                    <div class="time">昨天 23:36</div>
                  </div>
                </div>
                <div class="name-title">{{ item2.title }}</div>
                <div class="name-content">
                  <div class="content-note">
                    {{ item2.outlineDesc }}
                  </div>
                  <img class="img" :src="item2.userImage" alt="" />
                </div>
                <div class="name-foot">
                  <span
                    ><i class="iconfont">&#xe60c;</i>&nbsp;{{
                      item2.praiseCount
                    }}
                    点赞</span
                  >
                  <span
                    ><i class="iconfont">&#xe63b;</i>&nbsp;{{
                      item2.markCount
                    }}
                    评论</span
                  >
                </div>
              </div>
            </div>
          </van-list>
        </div>
      </van-pull-refresh>
    </div>
    <div v-else>
      <van-pull-refresh
        v-model="starRefresh"
        @refresh="onFrech"
        success-text="刷新成功"
      >
        <!-- 精贴 -->
        <div v-if="this.focusList.length !== 0">
          <!-- 热门榜 -->
          <van-list
            v-model="upLoading"
            :finished="finished"
            finished-text="没有更多了"
            @load="upLoad"
          >
            <div class="hot-content">
              <div
                class="item-content"
                v-for="(item3, index) in focusList"
                :key="index"
              >
                <div class="name-active">
                  <img class="img" :src="item3.userImage" alt="" />
                  <div class="N1">
                    <div class="name">{{ item3.userName }}</div>
                    <div class="time">昨天 23:36</div>
                  </div>
                </div>
                <div class="name-title">{{ item3.title }}</div>
                <div class="name-content">
                  <div class="content-note">
                    {{ item3.outlineDesc }}
                  </div>
                  <img class="img" :src="item3.userImage" alt="" />
                </div>
                <div class="name-foot">
                  <span
                    ><i class="iconfont">&#xe60c;</i>&nbsp;{{
                      item3.praiseCount
                    }}
                    点赞</span
                  >
                  <span
                    ><i class="iconfont">&#xe63b;</i>&nbsp;{{
                      item3.markCount
                    }}
                    评论</span
                  >
                </div>
              </div>
            </div>
          </van-list>
        </div>
        <div v-else>
          <div class="content1">
            <i class="iconfont">&#xe614;</i>
            <div>暂无关注</div>
          </div>
        </div>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import {
  communityColumn,
  goodNote,
  communityNoteList,
  communityNote
} from '@/api/find.js'
import { getLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      nameActive: 0, // 默认选中
      currentIndex: 0,
      ballList: [],
      goodnote: [],
      community: [],
      focusList: [],
      starLoading: false,
      upLoading: false,
      finished: false,
      starRefresh: false,
      current: 1,
      pageSize: 10
    }
  },
  async created () {
    const res = await communityColumn({})
    // console.log('res', res)
    this.ballList = res.data.data
    const res1 = await goodNote({
      pageNum: 1,
      pageSize: 4
    })
    // console.log('res1', res1)
    this.goodnote = res1.data.data.records
    const res2 = await communityNoteList({
      pageNum: 1,
      pageSize: 10,
      query: { noteDataType: 1, deviceId: 'e592781840bce064830f750616d116a5' }
    })
    this.community = res2.data.data.records
    // console.log('res2', res2)
  },
  methods: {
    // 刷新
    async onFrech () {
      if (this.nameActive === 0) {
        const res = await communityColumn({})
        this.ballList = res.data.data
        const res1 = await goodNote({
          pageNum: 1,
          pageSize: 4
        })
        this.goodnote = res1.data.data.records
        const res2 = await communityNoteList({
          pageNum: 1,
          pageSize: 10,
          query: {
            noteDataType: 1,
            deviceId: 'e592781840bce064830f750616d116a5'
          }
        })
        this.community = res2.data.data.records
        this.starLoading = false
      } else {
        const res3 = await communityNote({
          pageNum: this.current * this.pageSize,
          pageSize: this.pageSize,
          query: { noteDataType: 2 }
        })
        this.focusList = res3.data.data.records
        this.starRefresh = false
      }
    },
    // 加载
    async upLoad () {
      if (this.nameActive === 0) {
        const res2 = await communityNoteList({
          pageNum: this.current * this.pageSize,
          pageSize: this.pageSize,
          query: {
            noteDataType: 1,
            deviceId: 'e592781840bce064830f750616d116a5'
          }
        })
        this.community.push(...res2.data.data.records)
        this.upLoading = false
        this.current++
        if (this.community.length >= res2.data.data.total) {
          this.finished = true
        }
      } else if (this.nameActive === 1) {
        const res3 = await communityNote({
          pageNum: this.current * this.pageSize,
          pageSize: this.pageSize,
          query: { noteDataType: 2 }
        })
        this.focusList.push(...res3.data.data.records)
        this.upLoading = false
        this.current++
        if (this.focusList.length >= res3.data.data.total) {
          this.finished = true
        }
      }
    },
    // 关注
    async attention () {
      this.currentIndex = this.nameActive
      console.log('我点击了', this.currentIndex)
      if (this.nameActive === 1) {
        if (getLocal('token')) {
          const res3 = await communityNote({
            pageNum: this.current * this.pageSize,
            pageSize: this.pageSize,
            query: { noteDataType: 2 }
          })
          console.log('guanzhu', res3)
          this.focusList = res3.data.data.records
        } else {
          this.$router.push('/login')
        }
      }
    }
  },
  mounted () {}
}
</script>

<style lang="less">
.community {
  .tab {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid #ccc;
  }
  .hot-group {
    background-color: #fff;
    padding: 21px 16px 15px;
    .list {
      width: 100%;
      height: 58px;
      display: flex;
      align-items: center;
      justify-content: space-around;
      .item {
        text-align: center;
        flex-shrink: 0;
        .img {
          width: 34px;
          height: 34px;
        }
        .itemName {
          margin-top: 10px;
          font-size: 12px;
        }
      }
    }
  }
  .hot-post {
    width: 100%;
    background-color: #f7f8fa;
    padding: 10px 10px 10px 10px;
    .row-post {
      display: flex;
      align-items: center;
      width: 350px;
      height: 50px;
      background-color: #fff;
      border-radius: 5px;
      margin-top: 10px;
      .good {
        padding-left: 15px;
        .type {
          padding: 0 2px;
          border: 1px solid #2898ff;
          font-size: 14px;
          color: #2898ff;
          .iconfont {
            font-size: 14px;
            background-color: #2898ff;
            color: #fff;
          }
        }
      }
      .title {
        flex: 1;
        // padding-right: 15px;
        padding-left: 10px;
        font-size: 16px;
        font-weight: 600;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 1;
        -webkit-box-orient: vertical;
      }
    }
  }
  .hot-content {
    background-color: #f7f8fa;
    .item-content {
      margin-bottom: 15px;
      background-color: #fff;
      padding: 15px 15px;
      .name-active {
        display: flex;
        align-items: center;
        .img {
          width: 36px;
          height: 36px;
          border-radius: 50px;
        }
        .N1 {
          flex: 1;
          padding-left: 15px;
          .name {
            font-size: 14px;
            font-weight: 600;
          }
          .time {
            font-size: 14px;
            color: #9897ac;
          }
        }
      }
      .name-title {
        padding: 15px 0;
        color: #323838;
        font-size: 16px;
        font-weight: 500;
      }
      .name-content {
        color: #8c8c8c;
        font-size: 16px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        .img {
          margin-top: 10px;
          height: 186px;
        }
      }
      .name-foot {
        margin-top: 15px;
        color: #8c8c8c;
        font-size: 14px;
        span:nth-child(2) {
          padding-left: 15px;
        }
      }
    }
  }
  .content1 {
    text-align: center;
    // font-size: 20px;
    margin-top: 200px;
    .iconfont {
      font-size: 80px;
      color: #61b3ff;
    }
    div {
      font-size: 20px;
      color: #888888;
    }
  }
  .active {
    width: 56px;
    height: 28px;
    line-height: 28px;
    border-radius: 15px;
    background-color: #0085ff;
    color: #fff;
    text-align: center;
  }
}
</style>
